package com.tanhua.server.controller;

import com.tanhua.server.service.QuestionnaireService;
import com.tanhua.server.vo.Answer;
import com.tanhua.server.vo.QuestionnaireReportVo;
import com.tanhua.server.vo.QuestionnaireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("testSoul")
public class QuestionnaireController {
    @Autowired
    private QuestionnaireService questionnaireService;
    @Autowired
    private HttpServletRequest request;


    @GetMapping
    public ResponseEntity<List<QuestionnaireVo>> queryQusetionnaire() {
        try {
            List<QuestionnaireVo> questionnaireVos = this.questionnaireService.queryAll();
            return ResponseEntity.ok(questionnaireVos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping
    public ResponseEntity<String> commitQuestionnaire(@RequestBody Map<String,List<Answer>> param) {
        String reportId=null;

        try {
            List<Answer> answers = param.get("answers");
            for (Answer answer : answers) {
                reportId = this.questionnaireService.saveOptionsScore(answer.getQuestionId(), answer.getOptionId(),request);
            }
            return ResponseEntity.ok(reportId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @GetMapping("/report/{id}")
    public ResponseEntity<QuestionnaireReportVo> viewQuestionnaireReport(@PathVariable("id") String questionnaireReportId){
        try {
            QuestionnaireReportVo questionnaireReportVo = this.questionnaireService.queryQuestionnaireReportById(questionnaireReportId);
            return ResponseEntity.ok(questionnaireReportVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
